package com.example.emos.api.service.impl;

import cn.hutool.core.map.MapUtil;
import com.example.emos.api.common.util.PageUtils;
import com.example.emos.api.db.dao.TbLeaveDao;
import com.example.emos.api.db.pojo.TbLeave;
import com.example.emos.api.exception.EmosException;
import com.example.emos.api.service.LeaveService;
import com.example.emos.api.task.LeaveWorkflowTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;

@Service
public class LeaveServiceImpl implements LeaveService {

    @Autowired
    private TbLeaveDao leaveDao;
    @Autowired
    private LeaveWorkflowTask leaveWorkflowTask;

    @Override
    public PageUtils searchLeaveByPage(HashMap param) {
        ArrayList<HashMap> list = leaveDao.searchLeaveByPage(param);
        Long count = leaveDao.searchLeaveCount(param);
        int start = (Integer) param.get("start");
        int length = (Integer) param.get("length");
        PageUtils pageUtils = new PageUtils(list, count, start, length);
        return pageUtils;
    }

    @Override
    public boolean searchContradiction(HashMap param) {
        long count = leaveDao.searchContradiction(param);
        return count > 0;
    }

    @Override
    public int insert(TbLeave leave) {
        int rows = leaveDao.insert(leave);
        //开启工作流
        if(rows ==1){
            //todo 调用工作流创建请假工作流
        }else{
            throw new EmosException( "添加请假记录失败");
        }
        return rows;
    }

    @Override
    public int deleteLeaveById(HashMap param) {
        int id = MapUtil.getInt(param, "id");
        String instanceId = leaveDao.searchInstanceById(id);
        int rows = leaveDao.deleteLeaveById(param);
        if(rows ==1){
            leaveWorkflowTask.deleteLeaveWorkflow(instanceId,"员工请假","删除请假申请");
        }else{
            throw new EmosException("删除请假申请失败");
        }
        return rows;
    }
}
